// import { Share } from './shareModule.js'
import U from '../utils.js'
import { shareYouTube } from '../youtube/youtubeShare.js'
import '../jquery.nicescroll.js'
import { getList } from "../list.js";
// import {pageRequestObj} from './index.js';
let socialTotal = 0,typeTotal = 5;
let selectedSocial = [], searchType = "";
export default {
	domInit: function() {
        let _this = this;
        U.fnNiceScroll($(".share_body"));
        // 获取可分享账号的list
        _this.getShareSocialList();
        // 分享
        $(".shareBtn").on("click", function() {
            selectedSocial = $(".share_body").find(".is-checked");
            if(selectedSocial.length == 0) {
                $.message({message: "请先选择一个账号", type: "info"});
                return;
            }
            _this.toShare();
        });
        // 取消分享
        $(".quiteBtn").on("click", function() {
            _this.quiteShare();
        });
        // 全选
        $(".selectAllBtn").on("click", function() {
            let optionObj;
            let flag = $(this).hasClass("socialAll");
            $(this).toggleClass("is-checked");
            optionObj = flag? $(".share_body .shareIcon") : $(".canselect .dropdown_option");
            if($(this).hasClass("is-checked")) {
                optionObj.removeClass("is-checked").addClass("is-checked");
            }else{
                optionObj.removeClass("is-checked");
            }
            if(!flag) {
                _this.filterSocial();
            }
        });
        // 单独点击
        $(".canselect .dropdown_option").on("click", function() {
            $(this).toggleClass("is-checked");
            let selectAllObj = $(".selectAllBtn.dropdown_option");
            let selectedType = $(".canselect").find(".is-checked");
            let flag = selectedType.length == typeTotal;
            if(flag) {
                selectAllObj.removeClass("is-checked").addClass("is-checked");
            }else{
                selectAllObj.removeClass("is-checked");
            }
            _this.filterSocial();
        });
        // 筛选
        $(".dropdownDefault").on("click", function() {
            $(".dropdown_menu").toggleClass("hide");
        });
    },
    // 筛选social
    filterSocial: function() {
        let searchObj = $(".canselect").find(".is-checked");
        let searchLength = searchObj.length;
        if(searchLength == 0){
            searchType = "NULL";
        }else if(searchLength == 5) {
            searchType = "";
        }else{
            let selectSearchArr = [];
            for(let i = 0;i < searchLength; i++) {
                selectSearchArr.push(searchObj.eq(i).attr("data-type"));
            }
            searchType = selectSearchArr.join(",");
        }
        $(".typeTotal").html(searchLength);
        this.getShareSocialList();
    },
    // 搜索查询获取socialList列表
    getShareSocialList: function () {
        let _this = this;
        let params = {
            nickName: '',
            type: searchType, // 默认空表示搜所有类型,NULL表示未选中任何类型
            which: 'social',  // social: 代表social账号list  liveList: live视频list
            pageNum: 1,
            pageSize: 99999
        }
        // 获取列表数据的公共方法
        new getList(params, function(data){
            _this.setSocialList(data.list);
            socialTotal = data.list.length;
        });
    },
    setSocialList: function(arr) {
        $(".share_body").html("");
        let selectAllFlag = $(".socialAll").hasClass("is-checked");
        for(let i = 0; i < arr.length; i++) {
            let classN = "";
            let obj = arr[i];
            switch (obj.type) {
                case "F": classN = 'i-facebook';break;
                case "Y": classN = 'i-youtube';break;
                case "S": classN = 'i-periscope';break;
                case "O": classN = 'i-other';break;
                case "I": classN = 'i-other';break;
                case "Z": classN = 'i-yizhibo';break;
                default: classN = 'i-other';break;
            }
            let divObj = $("<div class='shareIcon' social-name="+ obj.nickName +"><span class='selfcheckbox selectItem' type='checkbox'></span></div>");
            let iconObj = $("<div class='label_content' title="+ obj.nickName +"><div class='iconfont'></div><p class='ellipsis'><span class='squre'></span>"+ obj.nickName +"</p></div>");
            iconObj.find(".iconfont").addClass(classN);
            if(selectAllFlag) {
                divObj.removeClass("is-checked").addClass("is-checked");
            }
            divObj.on("click", function() {
                $(this).toggleClass("is-checked");
                let selectAllObj = $(".selectAllBtn.socialAll");
                selectedSocial = $(".share_body").find(".is-checked");
                let flag = selectedSocial.length == socialTotal;
                if(flag) {
                    selectAllObj.removeClass("is-checked").addClass("is-checked");
                }else{
                    selectAllObj.removeClass("is-checked");
                }
            });
            divObj.append(iconObj);
            $(".share_body").append(divObj);
        }
    },
    // 多分享
	toShare: function () {
        // 先循环判断有多少账号正在进行直播的
        // 获取最大推流数
        // 判断数量是否超过允许的最大推流数
        // 推流数量=正在进行推流账号数量+选中的推流账号数量
        // 若超过最大数则提示最多能分享**个/The max length you can share is**
        // 判断版本号，视频创建时间与2016/6/17 16:10做比较
        // 判断每一个选中的项是否有youtube类型且为自动，若有则进行youtube相关接口判断，首先判断每一个账号是否都进行过认证

        // YouTube推流
        let nickNameArr = [];
        selectedSocial.addClass("active");
        for(let i=0; i < selectedSocial.length; i++) {
            nickNameArr.push(U._getV(selectedSocial.eq(i), "social-name"));
        }
        // 如果url添加参数,这种获取方式获取的结果可能不准确
        let parentId = window.location.search.split("&")[0].split("=")[1];
        let params = {
            rId: parentId,
            nickName: nickNameArr.join(","),
            rType: U._getPV(parentId, "r-type"),
            livePeerId: U._getPV(parentId, "live-peer-id"),
            oldR: true,
            buildDate: U._getPV(parentId, "build-date"),
            encoderIndex: 1,
            platformType: "Y"
        }
        // 正在推流需要注意样式问题
        new shareYouTube(params, function(flag) {
            if(flag) {
                selectedSocial.removeClass("active");
            }
        });
    },
    // 停止分享
	quiteShare: function() {

    }
}